home *** CD-ROM | disk | FTP | other *** search
/ APC & TCP 4 / APC & TCP 4.iso / games / publicdomain / a / attacks / sources / history.mod < prev    next >
Text File  |  1994-05-14  |  13KB  |  274 lines

  1. IMPLEMENTATION MODULE history;
  2.  
  3. (*$R-*) (* range checking OFF *)
  4.  
  5. FROM header
  6.   IMPORT   boardtype, boardrange, histnodetype, histnodeptrtype, historytype,
  7.            playertype;
  8. FROM Storage
  9.   IMPORT   ALLOCATE, DEALLOCATE;
  10. FROM TermInOut
  11.   IMPORT   WriteString, WriteLn, WriteCard;
  12.  
  13.  
  14. (*************************************************************************)
  15.  
  16. PROCEDURE InitHistory (VAR history : historytype;
  17.                        board : boardtype;  player : playertype) : BOOLEAN;
  18.  
  19. (*   This takes an uninitialized variable of history type and sets all    *)
  20. (* the appropriate things to an initial value.                            *)
  21. (*                                                                        *)
  22. (*   INPUT                                                                *)
  23. (*            history           Variable of history type.  It should be   *)
  24. (*                              unused.                                   *)
  25. (*                                                                        *)
  26. (*            board             This board should represent the current   *)
  27. (*                              board state.                              *)
  28. (*                                                                        *)
  29. (*            player            And similarly for the player.             *)
  30. (*                                                                        *)
  31. (*   OUTPUT                                                               *)
  32. (*            history           Same variable.  It will be modified so    *)
  33. (*                              that it will reflect an initial state.    *)
  34. (*                                                                        *)
  35. (*            The function will return TRUE only if it can properly in-   *)
  36. (*            itialize the variable.  If something goes wrong -> FALSE.   *)
  37. VAR
  38.   ptr : histnodeptrtype;
  39.  
  40. BEGIN
  41.   ALLOCATE(ptr, SIZE(histnodetype));
  42.   IF ptr = NIL THEN
  43.      RETURN FALSE;
  44.      END;
  45.   ptr^.board := board;
  46.   ptr^.turn := player;
  47.   ptr^.next := NIL;
  48.   ptr^.previous := NIL;
  49.   history.currentmove := ptr;
  50.   history.nummoves := 1;
  51.   RETURN TRUE;
  52. END InitHistory;
  53.  
  54. (**************************************************************************)
  55.  
  56. PROCEDURE AddToHistory (VAR history : historytype;
  57.                             board : boardtype;  player : playertype)
  58.                          : BOOLEAN;
  59.  
  60. (*   This takes an initialized variable of history type and adds the      *)
  61. (* board to it.  NOTE: it is up to the caller to maintain all the proper  *)
  62. (* changes to its own state.  This just keeps track of the moves.         *)
  63. (*                                                                        *)
  64. (*   INPUT                                                                *)
  65. (*            history           Variable of history type.  It definately  *)
  66. (*                              should be initialized.                    *)
  67. (*                                                                        *)
  68. (*            board             A boardtype.  It is what will be added    *)
  69. (*                              to the history.                           *)
  70. (*                                                                        *)
  71. (*            player            Whose turn it was to move at this board's *)
  72. (*                              configuration.                            *)
  73. (*                                                                        *)
  74. (*   OUTPUT                                                               *)
  75. (*            history           Same variable.  It will be modified so    *)
  76. (*                              that it will now hold the new board.      *)
  77. (*                                                                        *)
  78. (*            The function will return TRUE only if it can properly add   *)
  79. (*            the new board to the history.  If something goes wrong,     *)
  80. (*            then this will try to return the original history and       *)
  81. (*            return FALSE.                                               *)
  82.  
  83. VAR
  84.   ptr : histnodeptrtype;
  85.  
  86. BEGIN
  87.            (* This removes all the moves off the top *)
  88.   WHILE history.currentmove^.next # NIL DO
  89.      ptr := history.currentmove^.next;
  90.      history.currentmove^.next := ptr^.next;
  91.      DEALLOCATE(ptr, SIZE(histnodetype));
  92.      DEC(history.nummoves);
  93.      END;
  94.  
  95.            (* Here we add the new move to the data struct *)
  96.   ALLOCATE(ptr, SIZE(histnodetype));
  97.   IF ptr = NIL THEN
  98.      RETURN FALSE;
  99.      END;
  100.   ptr^.board := board;
  101.   ptr^.turn := player;
  102.  
  103.   ptr^.previous := history.currentmove;
  104.   ptr^.next := NIL;
  105.   history.currentmove^.next := ptr;
  106.   history.currentmove := ptr;
  107.   INC (history.nummoves);
  108.   RETURN TRUE;
  109. END AddToHistory;
  110.  
  111. (**************************************************************************)
  112.  
  113. PROCEDURE PopHistory (VAR history : historytype;
  114.                       VAR board : boardtype;  VAR player : playertype)
  115.                          : BOOLEAN;
  116.  
  117. (*   This takes an initialized variable of history type and removes the   *)
  118. (* most recent move from it.  The result is put into the variable board.  *)
  119. (* If there are no boards in the current history (ie, the history list    *)
  120. (* is empty) then the function will return FALSE.                         *)
  121. (*                                                                        *)
  122. (*   INPUT                                                                *)
  123. (*            history           Variable of history type.  It definately  *)
  124. (*                              should be initialized.                    *)
  125. (*                                                                        *)
  126. (*            board             A boardtype.  Initially it is garbage.    *)
  127. (*                                                                        *)
  128. (*            player            A playertype.  Also garbage at the start. *)
  129. (*                                                                        *)
  130. (*   OUTPUT                                                               *)
  131. (*            history           Same variable.  It will be modified so    *)
  132. (*                              that it no longer holds the returned      *)
  133. (*                              board.                                    *)
  134. (*                                                                        *)
  135. (*            board             This will hold the board that is re-      *)
  136. (*                              turned.  If the history contains no pre-  *)
  137. (*                              vious boards, then this variable is un-   *)
  138. (*                              changed.                                  *)
  139. (*                                                                        *)
  140. (*            player            Holds the player whose turn it is for the *)
  141. (*                              returned board.                           *)
  142. (*                                                                        *)
  143. (*                                                                        *)
  144. (*            The function will return TRUE only if it can properly pop   *)
  145. (*            a board from it.  If the history is empty, then the func-   *)
  146. (*            returns FALSE.                                              *)
  147.  
  148.  
  149. BEGIN
  150.   IF history.currentmove^.previous = NIL THEN
  151.      RETURN FALSE;
  152.      END;
  153.   history.currentmove := history.currentmove^.previous;
  154.   board := history.currentmove^.board;
  155.   player := history.currentmove^.turn;
  156.   RETURN TRUE;
  157. END PopHistory;
  158.  
  159.  
  160. (**************************************************************************)
  161. PROCEDURE UpHistory (VAR history : historytype;  VAR board : boardtype;
  162.                      VAR player : playertype) : BOOLEAN;
  163.  
  164. (*   This is used to REDO moves.  It is called and works very much like   *)
  165. (* PopHistory.                                                            *)
  166. (*                                                                        *)
  167. (*   INPUT                                                                *)
  168. (*            history           Variable of history type.  It definately  *)
  169. (*